package com.daixu.xutil.demo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.daixu.xutil.demo.BaseActivity;
import com.daixu.xutil.demo.R;
import com.daixu.xutil.demo.adapter.XListViewAdapter;
import com.daixu.xutil.demo.widget.XListView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangbo on 2016/2/19.
 */
@ContentView(R.layout.activity_xlistview)
public class XListViewActivity extends BaseActivity implements XListView.IXListViewListener, AdapterView.OnItemClickListener {
    @ViewInject(R.id.list_demo)
    private XListView mListView;
    private XListViewAdapter mAdapter;
    private List<String> mList = new ArrayList<>();
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setData();

        mListView.setPullLoadEnable(true);//上拉刷新
        //mListView.setPullRefreshEnable(false);//下拉刷新
        mAdapter = new XListViewAdapter(this, mList);
        mListView.setAdapter(mAdapter);
        mListView.setXListViewListener(this);
        mListView.setOnItemClickListener(this);

        mHandler = new Handler();
    }

    private void setData() {
        mList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mList.add("第" + i + "个");
        }
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                fetchingNewData();
                mAdapter.notifyDataSetChanged();
                Toast.makeText(XListViewActivity.this, "Refresh Finished!", Toast.LENGTH_SHORT).show();
                onLoad();
            }
        }, 2000);
    }

    private void fetchingNewData() {
        mList.add(0, "下拉刷新出来的数据");
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                simulateLoadMoreData();
                onLoad();
            }
        }, 2000);
    }

    private void simulateLoadMoreData() {
        loadMoreData();
        mAdapter.notifyDataSetChanged();
        Toast.makeText(this, "Load Finished!", Toast.LENGTH_SHORT).show();
    }

    private void loadMoreData() {

        List<String> moreList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            moreList.add("加载更多的数据");
        }
        mList.addAll(moreList);

    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("刚刚");
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mList.size() > 0) {
            Toast.makeText(this, "value: " + mList.get(position), Toast.LENGTH_SHORT).show();
        }
    }
}
